spacer
spacerLokken Logo
spacerLokken Binary Logo
spacer
spacer
spacer
spacer spacer spacer spacer
Regular Expressions
Language:   JavaScript
Author:   Jon Lokken
Date:   3-12-2015

Description:  
Use a regular expression to validate a credit card number before processing via the Luhn algorithm. Sample card numbers can be found at Paypal.


Credit Card Number:  
Escape String:  
Manual Eval:  

 Visa Card: Cards start with a 4 and have either 13 or 16 digits.
   RegExp: ^4[0-9]{12}(?:[0-9]{3})?$

 Master Cards: Cards start with a 51-55 and have 16 digits.
   RegExp: ^5[1-5][0-9]{14}$

 American Express: Cards start with a 34 or 37 and have 15 digits.
   RegExp: ^3[47][0-9]{13}$

 Diners Club: Cards start with a 300-305, 36 or 38 and have 14 digits. Some start with a 5 and have 16 digits. These are joint venture cards between Diners Club and Mastercard. The joint cards should be processed like a MasterCard.
   RegExp: ^3(?:0[0-5]|[68][0-9])[0-9]{11}$

 Discover: Discover Cards start with a 6011 or 65 and have 16 digits.
   RegExp: ^6(?:011|5[0-9]{2})[0-9]{12}$

 JCB: Cards start with a 2131 or 1800 and have 15 digits. Some start with a 35 and have 16 digits.
   RegExp: ^(?:2131|1800|35[0-9]{3})[0-9]{11}$


Luhn Algorithm:
function valid_credit_card(value) {
 if (/[^0-9-\s]+/.test(value)) return false;
  var nCheck = 0, nDigit = 0, bEven = false;
  value = value.replace(/\D/g, "");
  for (var n = value.length - 1; n >= 0; n--) {
   var cDigit = value.charAt(n),
    nDigit = parseInt(cDigit, 10);
   if (bEven) {
    if ((nDigit *= 2) > 9) nDigit -= 9;
  }
  nCheck += nDigit;
  bEven = !bEven;
 }
 return (nCheck % 10) == 0;
}


Test CardsCredit Card Type: Credit Card Number
American Express: 378282246310005
American Express: 371449635398431
American Express Corporate: 378734493671000
Australian BankCard: 5610591081018250
Diners Club: 30569309025904
Diners Club: 38520000023237
Discover: 6011111111111117
Discover: 6011000990139424
JCB: 3530111333300000
JCB: 3566002020360505
MasterCard: 5555555555554444
MasterCard: 5105105105105100
Visa: 4111111111111111
Visa: 4012888888881881
Visa: 4222222222222Note : Even though this number has a different character count than the other test numbers, it is the correct and functional number.Processor-specific Cards
Dankort (PBS): 76009244561
Dankort (PBS): 5019717010103742
Switch/Solo (Paymentech): 6331101999990016

Disclaimer:
Feel free to use this tool.  I take no responsibility for this code working, imply no warranty as to it's validity, and will not be held liable if you decide to try it. 

Associated Sites: Jon Lokken - http://www.jonlokken.com | Amy Lokken - http://www.amylokken.com | Isabel Lokken - http://www.isabellokken.com | Elizabeth Lokken - http://www.elizabethlokken.com | Lokken.us - http://www.lokken.us | Lokken.co - http://www.lokken.co | Lokken Consulting - http://www.lokkenconsulting.com
Author: Jon Lokken - http://www.lokken.net